import React from "react";

import PageContent from '../../PageContent'

import { ajax ,showResultMessage} from '../../common';

import {UIDataTableView} from '../../uicomponents/UIDataComponentView';

import UIFormView from '../../uicomponents/UIFormView';

/** 
 * 产品模型数据类型管理
*/
export default class ProductModelDataTypeManageApp extends React.Component {

	render() {
		return (
			<PageContent {...this.props} pageInstance={this} >
				<ProductModelDataTypeListPanel />
			</PageContent>
		);
	}
}

/** 
 * 产品模型数据类型列表
*/
class ProductModelDataTypeListPanel extends React.Component {

	constructor(props) {
		super(props);
		this.state = {
			formTitle: '',
		}
	}


	actionHandler = (actionEvent) => {

		const { dataTypeListRef, dataTypeFormRef, selectdataTypeIds } = this;

		return {
			refresh: () => {
				dataTypeListRef.loadData();
			},
			addNewDataType: () => {
				this.setState({ formTitle: '新增数据类型' });
				setTimeout(() => {
					dataTypeFormRef.openModal();
				});
			},
			editDataType: () => {
				this.setState({ formTitle: '编辑数据类型' });
				setTimeout(() => {
					dataTypeFormRef.openModal(() => {
						dataTypeFormRef.setFormData(actionEvent.params);
					});
				});
			},
			doSave: () => {
				dataTypeFormRef.getFormData((formData) => {

					ajax('PRODUCT/MODEL/productModelDataType/saveDataType', formData, (data) => {
						dataTypeFormRef.closeModal();
						dataTypeListRef.loadData();
						showResultMessage({
							success: true,
							message: 'message.saveSuccess',
						});
					});
				});
			},
			deleteDataTypes: () => {
				let selectedIds = null;

				if (actionEvent.params != null) {
					selectedIds = [actionEvent.params.id];
				}
				else {
					selectedIds = selectdataTypeIds;
				}

				ajax('PRODUCT/MODEL/productModelDataType/deleteDataType', { ids: selectedIds }, (data) => {
					dataTypeListRef.loadData();
				});
			}
		};
	}

	render() {

		const dataTypeTableProp = {
			ref: (inst) => { this.dataTypeListRef = inst },
			actionHandler: this.actionHandler,
			actionGroupId: 'dataTypeListActionGroup',
			rowActionGroupId: 'dataTypeRowActionGroup',
			tableConfigId: 'dataTypeList',
			dataUrl: 'PRODUCT/MODEL/productModelDataType/queryDataTypeList',
			selectMode: 'multi',
			searchFormId: 'dataTypeQueryForm',
			autoLoad: true,
			setToolbarState: (topToolBar, selectedIds) => {
				if (selectedIds.length > 0) {
					topToolBar.enable('delete');
					topToolBar.enable('edit');
				}
				else {
					topToolBar.disable('delete');
					topToolBar.disable('edit');
				}
			},
			onSelectChange:
				(selectRowKeys) => {
					this.selectdataTypeIds = selectRowKeys;
				}
			,
		};

		const dataTypeFormProps = {
			ref: (inst) => { this.dataTypeFormRef = inst },
			formConfigId: 'dataTypeForm',
			actionGroupId: 'saveActionGroup',
			actionHandler: this.actionHandler,
			inPop: true,
			destroyOnClose: true,
			title: this.state.formTitle,
		};

		return (
			<UIDataTableView {...dataTypeTableProp}>
				<UIFormView {...dataTypeFormProps} />
			</UIDataTableView>
		);
	}
}